home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / what.zip / EWDEMO.C_ / EWDEMO.bin
Text File  |  1992-07-15  |  14KB  |  388 lines

  1. /*****************************************************************************
  2. *                                                                            *
  3. *  EWDEMO.C                                     *
  4. *                                                                            *
  5. *  Copyright (C) Microsoft Corporation 1991.                     *
  6. *  All Rights reserved.                                                      *
  7. *                                                                            *
  8. ******************************************************************************
  9. *                                                                            *
  10. *  Module Description: Implements an embedded window DLL that lists the      *
  11. *               current system printers.                  *
  12. *                                                                            *
  13. *****************************************************************************/
  14.  
  15.  
  16. #define NOMINMAX
  17. #include <windows.h>
  18. #include "dll.h"
  19. #include "dlldemo.h"
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24.  
  25. #ifndef EXPORT
  26. #define EXPORT FAR PASCAL
  27. #endif
  28. #define PRIVATE static
  29.  
  30. #define MAX_BUFFER      128
  31. #define MAX_PRINTERS      10
  32. #define MAX_PRINTERLINE   40
  33.  
  34.  
  35. /*****************************************************************************
  36. *                                                                            *
  37. *                            Global Variables                                *
  38. *                                                                            *
  39. *****************************************************************************/
  40.  
  41. HANDLE ghModule;                /* Handle to DLL */
  42. char   szPLClassName[] = "PrinterList";     /* printer list class name */
  43. char   rgchBuffer[MAX_BUFFER];
  44. char   rgrgchPrinters[MAX_PRINTERS][MAX_PRINTERLINE];
  45.  
  46. /*****************************************************************************
  47. *                                                                            *
  48. *                               Prototypes                                   *
  49. *                                                                            *
  50. *****************************************************************************/
  51.  
  52. long EXPORT PrinterListProc (HWND hwnd,
  53.                  UINT wMsg,
  54.                  WPARAM wParam,
  55.                  LPARAM lParam);
  56.  
  57. void NEAR PASCAL InitPrinterList( void );
  58.  
  59. /*****************************************************************************
  60. *                                                                            *
  61. *                               Functions                                    *
  62. *                                                                            *
  63. *****************************************************************************/
  64.  
  65. /***************************************************************************
  66.  *
  67.  -  Name    FInitEmbeddedWindow
  68.  -
  69.  *  Purpose    Called by LibMain during initialization of DLL.
  70.  *
  71.  *  Arguments    hModule     Module handle for the library
  72.  *
  73.  *  Returns     
  74.  *
  75.  *  +++
  76.  *
  77.  *  Notes
  78.  *
  79.  ***************************************************************************/
  80.  
  81. BOOL FInitEmbeddedWindow( HANDLE hModule )
  82.   {
  83.   WNDCLASS wc;
  84.   ghModule = hModule;
  85.  
  86.   wc.lpszClassName  = szPLClassName;
  87.   wc.style          = CS_GLOBALCLASS;
  88.   wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  89.   wc.hIcon          = NULL;
  90.   wc.lpszMenuName   = NULL;
  91.   wc.hbrBackground  = COLOR_WINDOW + 1;
  92.   wc.hInstance      = hModule;
  93.   wc.lpfnWndProc    = PrinterListProc;
  94.   wc.cbClsExtra     = 0;
  95.   wc.cbWndExtra     = 0;
  96.  
  97.   if (!RegisterClass (&wc))
  98.     return FALSE; /* Initialization  failed */
  99.  
  100.   return TRUE;  /* if we got here everything is ok! */
  101.   }
  102.  
  103.  
  104. /***************************************************************************
  105.  *
  106.  -  Name    PrinterListProc
  107.  -
  108.  *  Purpose    Maintains a window with a list of the current system printers.
  109.  *
  110.  *  Arguments    hwnd    This window's handle
  111.  *        wMsg    The current message
  112.  *        wParam    Message dependent
  113.  *        lParam    Message dependent
  114.  *
  115.  *  Returns    Message dependent
  116.  *
  117.  *  +++
  118.  *
  119.  *  Notes
  120.  *
  121.  ***************************************************************************/
  122.  
  123. long EXPORT PrinterListProc( HWND hwnd,
  124.                  UINT wMsg,
  125.                  WPARAM wParam,
  126.                  LPARAM lParam )
  127.   {
  128.   PAINTSTRUCT ps;
  129.   HANDLE      gh;
  130.   HBITMAP     hbm;
  131.   HBITMAP     hbmDefault;
  132.   HBRUSH      hbrush;
  133.   HDC         hdc;
  134.   HFONT       hfont;
  135.   int          irgch;
  136.   long        lReturn;
  137.   POINT       pt;
  138.   QCI          qci;
  139.   QRI          qri;
  140.   RECT          rc;
  141.   static char rgchHelpFile[_MAX_PATH];
  142.   static char rgchAuthorText[50];
  143.   LPSTR       sz;
  144.   TEXTMETRIC  tm;
  145.  
  146.   switch (wMsg)
  147.     {
  148.     case WM_CREATE:
  149.       qci = (QCI)((CREATESTRUCT FAR *)lParam)->lpCreateParams;
  150.  
  151.       /*------------------------------------------------------------*\
  152.       | Save the WM_CREATE information from WinHelp.
  153.       \*------------------------------------------------------------*/
  154.       lstrcpy( rgchHelpFile, qci->szFileName );
  155.       if (lstrlen( qci->szAuthorData ) < 50)
  156.         lstrcpy( rgchAuthorText, qci->szAuthorData );
  157.  
  158.       /*------------------------------------------------------------*\
  159.       | Add a border to this window.
  160.       \*------------------------------------------------------------*/
  161.       SetWindowLong( hwnd, GWL_STYLE,
  162.              GetWindowLong( hwnd, GWL_STYLE ) | WS_BORDER );
  163.  
  164.       /*------------------------------------------------------------*\
  165.       | Initialize the printer list.  This could be updated more
  166.       | dynamically, say on each WM_PAINT message.
  167.       \*------------------------------------------------------------*/
  168.       InitPrinterList();
  169.  
  170.       return 0L;
  171.  
  172.     case WM_PAINT:
  173.       BeginPaint( hwnd, &ps );
  174.       GetTextMetrics( ps.hdc, &tm );
  175.       SetTextColor( ps.hdc, GetSysColor( COLOR_WINDOWTEXT ) );
  176.       SetBkColor( ps.hdc, GetSysColor( COLOR_WINDOW ) );
  177.       for (irgch = 0; irgch < MAX_PRINTERS && rgrgchPrinters[irgch][0];
  178.        irgch++)
  179.     TextOut( ps.hdc, tm.tmMaxCharWidth/2,
  180.          (tm.tmHeight + tm.tmExternalLeading)/2 +
  181.           irgch*(tm.tmHeight + tm.tmExternalLeading),
  182.          rgrgchPrinters[irgch], lstrlen( rgrgchPrinters[irgch] ) );
  183.       EndPaint( hwnd, &ps );
  184.       return 0L;
  185.  
  186.     case EWM_RENDER:
  187.       /*-----------------------------------------------------------------*\
  188.       * Render message from WinHelp
  189.       *   wParam is the type of rendering requested.
  190.       *   lParam is a pointer to a RENDERINFO structure
  191.       *   Return the supported rendering.
  192.       \*-----------------------------------------------------------------*/
  193.       switch( wParam )
  194.         {
  195.         case CF_BITMAP:
  196.       /*------------------------------------------------------------*\
  197.       | Prepare a bitmap image of the printer list.  This will
  198.       | appear in a similar manner as the layout, but we will need
  199.       | to draw the border explicitly here, as well as making sure
  200.       | the background is filled in correctly.  We must use the
  201.       | default colors for this hdc
  202.       \*------------------------------------------------------------*/
  203.       qri = (QRI)lParam;
  204.           hdc = CreateCompatibleDC( NULL );
  205.  
  206.       if (hdc)
  207.             {
  208.             hfont = 0;
  209.  
  210.             /*------------------------------------------------------------*\
  211.             | Create a monochrome bitmap for the DC, sized for the screen.
  212.             \*------------------------------------------------------------*/
  213.             SendMessage( hwnd, EWM_QUERYSIZE, hdc, (long)(LPPOINT)&pt );
  214.             rc.left = 0;
  215.             rc.top = 0;
  216.             rc.right = pt.x;
  217.             rc.bottom = pt.y;
  218.  
  219.             hbm = CreateCompatibleBitmap( qri->hdc, pt.x, pt.y );
  220.             if (hbm)
  221.               {
  222.               hbmDefault = SelectObject( hdc, hbm );
  223.               /*------------------------------------------------------------*\
  224.               | Clear out the bitmap
  225.               \*------------------------------------------------------------*/
  226.               hbrush = CreateSolidBrush( GetBkColor( hdc ) );
  227.               if (hbrush)
  228.                 {
  229.                 FillRect( hdc, &rc, hbrush );
  230.                 DeleteObject( hbrush );
  231.                 }
  232.  
  233.               Rectangle( hdc, rc.left, rc.top, rc.right, rc.bottom );
  234.  
  235.               GetTextMetrics( hdc, &tm );
  236.               for (irgch = 0; irgch < MAX_PRINTERS && rgrgchPrinters[irgch][0];
  237.                    irgch++)
  238.                 TextOut( hdc, tm.tmMaxCharWidth/2,
  239.                          (tm.tmHeight + tm.tmExternalLeading)/2 +
  240.                           irgch*(tm.tmHeight + tm.tmExternalLeading),
  241.                          rgrgchPrinters[irgch],
  242.                          lstrlen( rgrgchPrinters[irgch] ) );
  243.               /*----------------------------------------------------------*\
  244.               | At this point, hbm is the desired bitmap, sized for a
  245.               | screen display.  This will be stretched as needed for
  246.               | the target display by WinHelp.
  247.               \*----------------------------------------------------------*/
  248.               hbm = SelectObject( hdc, hbmDefault );
  249.               }
  250.             else
  251.               {
  252.               /*------------------------------------------------------------*\
  253.               | Not enough memory.  Cleanup and return NULL.
  254.               \*------------------------------------------------------------*/
  255.               hbm = NULL;
  256.               }
  257.  
  258.             DeleteDC( hdc );
  259.         }
  260.  
  261.  
  262.           lReturn = (long)hbm;
  263.           break;
  264.  
  265.         case CF_TEXT:
  266.       /*------------------------------------------------------------*\
  267.       | List out the printers in a format suitable for the clipboard.
  268.       | Since this list will be embedded in the text of the topic,
  269.       | use blank lines for separators.
  270.       \*------------------------------------------------------------*/
  271.       gh = GlobalAlloc( GMEM_MOVEABLE | GMEM_NOT_BANKED,
  272.                 sizeof(rgrgchPrinters) );
  273.           lReturn = (long)gh;
  274.           if (gh)
  275.             {
  276.         sz = GlobalLock( gh );
  277.         lstrcpy( sz, "\r\n" );
  278.         for (irgch = 0; irgch < MAX_PRINTERS && rgrgchPrinters[irgch][0];
  279.          irgch++)
  280.           {
  281.           lstrcat( sz, rgrgchPrinters[irgch] );
  282.           lstrcat( sz, "\r\n" );
  283.           }
  284.             GlobalUnlock( gh );
  285.             }
  286.           break;
  287.  
  288.         default:
  289.           lReturn = 0;
  290.         }
  291.  
  292.       return lReturn;
  293.  
  294.     case EWM_QUERYSIZE:
  295.       /*-----------------------------------------------------------------*\
  296.       * Size query message from WinHelp
  297.       *   wParam is the target hdc.
  298.       *   wLong is the address of the point to return size in.
  299.       *   Return non-zero to indicate that we did something.
  300.       \*-----------------------------------------------------------------*/
  301.       hfont = SelectObject( (HDC)wParam, GetStockObject( SYSTEM_FONT ) );
  302.       GetTextMetrics( (HDC)wParam, &tm );
  303.       ((LPPOINT)lParam)->x = ((LPPOINT)lParam)->y = 0;
  304.       for (irgch = 0; irgch < MAX_PRINTERS && rgrgchPrinters[irgch][0];
  305.      irgch++)
  306.     {
  307.     DWORD dwExt = GetTextExtent( (HDC)wParam, rgrgchPrinters[irgch], 
  308.       lstrlen( rgrgchPrinters[irgch] ) );
  309.     ((LPPOINT)lParam)->x = max( (WORD)((LPPOINT)lParam)->x, LOWORD(dwExt) );
  310.     ((LPPOINT)lParam)->y += HIWORD(dwExt);
  311.     }
  312.       ((LPPOINT)lParam)->x += tm.tmMaxCharWidth;
  313.       ((LPPOINT)lParam)->y += tm.tmHeight + tm.tmExternalLeading;
  314.       if (hfont)
  315.         SelectObject( (HDC)wParam, hfont );
  316.       return 1;
  317.     }
  318.  
  319.     return DefWindowProc (hwnd, wMsg, wParam, lParam);
  320.   }
  321.  
  322. /*****************************************************************************
  323. *                                                                            *
  324. *                 Local Functions                     *
  325. *                                                                            *
  326. *****************************************************************************/
  327.  
  328. /***************************************************************************
  329.  *
  330.  -  Name    InitPrinterList
  331.  -
  332.  *  Purpose    Initializes the list of printers in the system
  333.  *
  334.  *  Arguments    none
  335.  *
  336.  *  Returns    nothing
  337.  *
  338.  *  +++
  339.  *
  340.  *  Notes    Uses the global variable rgrgchPrinters to store the system
  341.  *        printer list.
  342.  *
  343.  ***************************************************************************/
  344.  
  345. void NEAR PASCAL InitPrinterList( void )
  346.   {
  347.   int          irgch;
  348.   char         *pch;
  349.   char         *pchKey;
  350.   char         *pchTemp;
  351.  
  352.   /*------------------------------------------------------------*\
  353.   | These must be static, since we use near pointers into them,
  354.   | and in a DLL DS!=SS.
  355.   \*------------------------------------------------------------*/
  356.   static char rgchTemp[MAX_PRINTERLINE];
  357.   static char rgchDevice[MAX_PRINTERLINE];
  358.  
  359.   GetProfileString( "windows", "device", "", rgchDevice,
  360.             MAX_PRINTERLINE );
  361.   for (pch = rgchDevice; *pch != ',' && *pch != '\0'; pch++)
  362.     ;
  363.   *pch = '\0';
  364.  
  365.   GetProfileString( "PrinterPorts", NULL, "", rgchBuffer, MAX_BUFFER );
  366.   for (pchKey = rgchBuffer, irgch = 0; *pchKey && irgch < MAX_PRINTERS;
  367.        pchKey += lstrlen( pchKey ) + 1, irgch++)
  368.     {
  369.     lstrcpy( rgrgchPrinters[irgch], pchKey );
  370.     lstrcat( rgrgchPrinters[irgch], " on " );
  371.     GetProfileString( "PrinterPorts", pchKey, "",
  372.               rgchTemp, MAX_PRINTERLINE );
  373.     for (pch = rgchTemp; *pch != ',' && *pch != '\0'; pch++)
  374.       ;
  375.     if (*pch == ',')
  376.       {
  377.       pchTemp = pch + 1;
  378.       for (pch = pchTemp; *pch != ',' && *pch != '\0'; pch++)
  379.     ;
  380.       *pch = '\0';
  381.       lstrcat( rgrgchPrinters[irgch], pchTemp );
  382.       lstrcat( rgrgchPrinters[irgch], "," );
  383.       lstrcat( rgrgchPrinters[irgch],
  384.            lstrcmp( pchKey, rgchDevice ) ? "inactive" : "active" );
  385.       }
  386.     }
  387.   }
  388.